<template>
    <n-drawer :show="active" :width="width" :mask-closable="false" @update:show="(flag:boolean) => (active = flag)">
        <n-drawer-content :native-scrollbar="false" title="价格设置" closable>
            <n-space vertical :size="24" v-if="model.id">
                <n-form label-placement="left" inline :show-feedback="false">
                    <n-form-item label="车辆信息">
                        <n-space>
                            <b>{{ item.cno }}</b>
                            <b>{{ item.name }}</b>
                        </n-space>
                    </n-form-item>
                </n-form>
                <n-form label-placement="left" :model="model" :rules="rules" ref="formRef">
                    <n-space vertical :size="24">
                        <n-grid :cols="24" :x-gap="24">
                            <n-form-item-gi :span="24" label="车辆押金" path="deposit">
                                <n-space :align="`center`">
                                    <DecimalInput :precision="2" suffix="元" :border="true" :min="0" v-model:base="model.price.deposit" />
                                    <span>/ 单</span>
                                </n-space>
                            </n-form-item-gi>
                            <n-form-item-gi :span="24" label="终端价格" path="mdp">
                                <n-space :align="`center`">
                                    <DecimalInput :precision="2" suffix="元" :border="true" :min="0" v-model:base="model.price.mdp" />
                                    <span>/ 日</span>
                                </n-space>
                            </n-form-item-gi>
                            <n-form-item-gi :span="24" label="调车价格" path="pmp">
                                <n-space :align="`center`">
                                    <DecimalInput :precision="2" suffix="元" :border="true" :min="0" v-model:base="model.price.pdp" />
                                    <span>/ 日</span>
                                    <DecimalInput :precision="2" suffix="元" :border="true" :min="0" v-model:base="model.price.pmp" />
                                    <span>/ 月</span>
                                </n-space>
                            </n-form-item-gi>
                            <!-- <n-form-item-gi :span="24" label="显示价格" path="disp">
                                <n-space :align="`center`">
                                    <DecimalInput :precision="2" suffix="元" :border="true" :min="0" v-model:base="model.price.disp" />
                                    <span>/ 日</span>
                                </n-space>
                            </n-form-item-gi> -->
                        </n-grid>
                    </n-space>
                </n-form>
            </n-space>

            <template #footer>
                <n-space justify="end">
                    <n-button type="tertiary" :disabled="loading" @click="() => (active = false)">
                        <template #icon>
                            <n-icon :component="CloseRound" />
                        </template>
                        关闭
                    </n-button>

                    <n-button type="primary" :loading="loading" @click="execSave">
                        <template #icon>
                            <n-icon :component="CheckRound" />
                        </template>
                        提交保存
                    </n-button>
                </n-space>
            </template>
        </n-drawer-content>
    </n-drawer>
</template>

<script lang="ts" setup>
import { computed, ref, onBeforeMount } from 'vue'
import { CloseRound, CheckRound } from '@vicons/material'

import { Http } from '@/libs'
import { useStore } from '@/store'

import DecimalInput from '@/comp/func/DecimalInput.vue'

const store = useStore()
const width = computed(() => Math.max(store.state.browser.innerWidth * 0.4, 640))

const { $notification: notification } = window

const loading = ref(false)

const prop = defineProps({
    show: {
        type: Boolean,
        required: true,
    },
    item: {
        type: Object,
        default: {},
    },
})

const emit = defineEmits(['update:show', 'refresh'])

const active = computed({
    get: () => prop.show,
    set: val => emit('update:show', val),
})

const formRef = ref<any>(null)

const model = ref<any>({})

const rules = {
    // name: {
    //     required: true,
    //     trigger: ['blur', 'input'],
    //     message: '请填写车型名称',
    // },
}

const execReset = () => {
    model.value = {
        id: prop.item.id,
        price: {
            mdp: prop.item.price.mdp,
            pdp: prop.item.price.pdp,
            pmp: prop.item.price.pmp,
            deposit: prop.item.price.deposit,
            disp: prop.item.price.disp,
        },
    }
}

const execSave = (e: Event) => {
    e.preventDefault()
    formRef.value.validate((errors: any[]) => {
        if (!errors) {
            loading.value = true
            Http.post('/ops/biz/car/price/set', model.value).then(resp => {
                if (resp.stat.err > 0) {
                    loading.value = false
                    return
                }
                emit('refresh', {})
                notification.success({
                    duration: 1500,
                    title: '操作成功',
                    content: '您已成功保存租金价格',
                })
                setTimeout(() => {
                    loading.value = false
                    emit('update:show', false)
                }, 200)
            })
        } else {
            let msgs = errors.map(err => err[0].message)
            notification.error({
                duration: 1500,
                title: '请填写完整内容',
                description: '当前表单存在以下问题，请处理后提交',
                content: msgs.join('\n'),
            })
        }
    })
}
onBeforeMount(() => {
    execReset()
})
</script>
